<template>
  <q-page class="page-map row bg-white">
    <q-scroll-area class="bg-green-1" style="width: 214px">
      <div class="q-pa-sm">
        <div>
          <q-btn style="position: relative; float: right" icon="add" no-wrap color="primary" label="新建" @click="addTopic" />
        </div>
        <div style="clear: both"></div>
        <q-tree :nodes="simple" default-expand-all dense node-key="value" tick-strategy="leaf" :ticked.sync="topics" :selected.sync="selectedKey" @update:ticked="uploadTopic" :expanded.sync="expanded" />
      </div>
    </q-scroll-area>
    <div class="col" style="display: flex; flex-direction: column">
      <Forum :treeNodes="treeNodes" style="flex-shrink: 0; flex-grow: 0"></Forum>
      <ForumList :topicsString="topicsString"></ForumList>
    </div>
    <q-scroll-area class="bg-green-1" style="width: 214px">
      <div class="q-pa-sm">
        <ForumTop :forumTopList="forumTopList" @showFDialog="showDialog"></ForumTop>
      </div>
      <div style="float: right; margin-right: 20px">
        <q-btn round color="primary" icon="mdi-robot-outline" @click="showAiDialog" />
      </div>
    </q-scroll-area>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          新增话题
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5>名称：</h5>
              <q-input outlined dense v-model="form.label" type="text" />
            </div>
            <div class="col-12">
              <h5>值：</h5>
              <q-input outlined dense v-model="form.value" type="text" />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <ForumTopDialog ref="topForum"></ForumTopDialog>
    <ChatComponent ref="ai"></ChatComponent>
  </q-page>
</template>

<script>
import axios from 'axios';
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import ForumList from './forumlist.vue';
import Forum from './forum.vue';
import ForumTop from './forum-top.vue';
import ForumTopDialog from './top-form-list.vue';
import ChatComponent from './ChatComponent.vue';

export default {
  name: 'forumn',
  components: {
    Forum,
    ForumList,
    ForumTop,
    ForumTopDialog,
    ChatComponent,
  },
  data() {
    return {
      topics: [],
      selectedKey: '',
      treeNodes: [],
      topicsString: '',
      forumTopList: [],
      expanded: ['Satisfied customers (with avatar)', 'Good food (with icon)'],
      form: {
        label: '',
        value: '',
      },
      emptyForm: {
        label: '',
        value: '',
      },
      simple: [
        {
          label: '话题',
          avatar: 'https://cdn.quasar.dev/img/boy-avatar.png',
          children: [
            {
              label: '计算机',
              value: '01',
              children: [
                {
                  label: '数据结构',
                  value: '0101',
                },
                {
                  label: '计算机网络技术',
                  value: '0102',
                },
                {
                  label: '数据结构',
                  value: '0103',
                },
                {
                  label: '操作系统',
                  value: '0104',
                },
              ],
            },
            {
              label: '数学',
              value: '02',

              children: [
                {
                  label: '高等数学',
                  value: '0201',
                },
                {
                  label: '线性代数',
                  value: '0202',
                },
              ],
            },
          ],
        },
      ],
      url: {
        list: '/topic/list',
        getTopicTree: '/topic/getTopicTree',
        add: '/topic/add',
        edit: '/topic/edit',
        copy: '/topic/copy',
        delete: '/topic/delete',
        deleteBatch: '/topic/deleteBatch',
        exportXlsUrl: '/topic/exportXls',
        importExcelUrl: '/topic/importExcel',
      },
    };
  },
  methods: {
    showDialog(forumId) {
      this.$refs.topForum.show(forumId);
    },
    reset() {
      this.form = JSON.parse(JSON.stringify(this.emptyForm));
    },
    submit() {
      this.form.parentId = this.selectedKey;
      this.$axios
        .post(this.url.add, this.form)
        .then((r) => {
          this.$info(r.message);
          this.$refs.dialog.hide();
        })
        .finally(() => {
          this.query();
        });
    },
    addTopic() {
      this.reset();
      this.$refs.dialog.show();
    },
    uploadTopic() {},
    query() {
      this.$axios
        .get(this.url.getTopicTree, {
          params: {
            key: this.key,
          },
        })
        .then((r) => {
          this.simple[0].children = r.result;
          this.treeNodes = this.getLeafNodes(this.simple);
          this.topics = this.treeNodes.map((item) => item.value);
        });
    },
    getLeafNodes(tree) {
      const result = [];
      function traverse(node) {
        if (!node.children || node.children.length === 0) {
          // 如果当前节点没有子节点，将其添加到结果列表中
          result.push(node);
        } else {
          // 若有子节点，递归遍历子节点
          node.children.forEach((child) => traverse(child));
        }
      }
      if (Array.isArray(tree)) {
        tree.forEach((node) => traverse(node));
      } else {
        traverse(tree);
      }
      return result;
    },
    countUserIdByForumId() {
      this.$axios.get('/user/forumLike/list').then((res) => {
        this.forumTopList = res.result;
        console.log(this.forumTopList);
      });
    },
    showAiDialog() {
      this.$refs.ai.$refs.dialog.show();
    },
  },
  mounted() {
    this.query();
  },
  created() {
    this.countUserIdByForumId();
  },
  computed: {},
  watch: {
    topics(newValue, oldValue) {
      this.topicsString = newValue.join(',');
    },
  },
};
</script>

<style lang="stylus" scope></style>
